import Vue from "vue";
import App from "./App.vue";
//vant
import { Dialog, Toast, Empty, PullRefresh } from "vant";
//mycomponent
import MyVanskeleton from "@/components/MyVanskeleton.vue";
import MyEmpty from '@/components/MyEmpty'
//utils
import { checkPersonLogin, checkCompanyLogin, getPersonID, getPersonAutoLoginData, GetAPPUserType, getCompanyID, getToken } from "@/util/session";
import api from '@/util/api'
import encrypt from "./util/encrypt";
import dayjs from "dayjs";
import myJmessage from "./util/myJmessage";
//路由
import VueRouter from "vue-router";
import Navigation from "vue-navigation";
import router from "./router";
//mixin
import { mixin_back, mixin_person } from "./mixin/mixins";
import MyMixin from "./mixin/MyMixin";

//chathub
import chathub from '@/util/chatHub'

Vue.component('MyVanskeleton', MyVanskeleton)
Vue.component('MyEmpty', MyEmpty)
Vue.use(Dialog)
Vue.use(Toast)
Vue.use(Empty)
Vue.use(PullRefresh)
Vue.mixin(mixin_back);
Vue.mixin(MyMixin);
Vue.use(VueRouter);
Vue.use(Navigation, { router, keyName: "sid" });

Vue.config.productionTip = false;
Vue.prototype.$dayjs = dayjs; // dayjs(date).format(template);
Vue.prototype.$api = api;
Vue.prototype.$chathub = chathub;

// myJmessage.init();

(async () =>{
	await api.get('/BaseData/GetBaseDataAll').then(res => {
		//decrpt
		let decrptStr = encrypt.AESdecrypt(res);
		Vue.prototype.$bd = JSON.parse(decrptStr);
	})
	new Vue({
		render: (h) => h(App),
		router: router,
		mixins: [mixin_person,MyMixin],
		created(){
			Vue.prototype.$bus = this; //注册全局总线 订阅后记得在beforedestory中销毁
			Vue.prototype.$api.initContext(this)
			// myJmessage.initContext(this);
			// if(GetAPPUserType() == 'P')
			// 	chathub.init(this,getPersonID(),getToken());
			// else if(GetAPPUserType() == 'C')
			// 	chathub.init(this,getCompanyID(),getToken());
			// else
			// 	chathub.init(this,'')
		},
		mounted(){
			document.dispatchEvent(new Event('render-event'))
		}
	}).$mount("#app");
})();


//前置路由守卫 主要用于判断权限
router.beforeEach((to, from, next) => {
	if (to.fullPath.indexOf("/person") >= 0 && to.meta.requiresAuth) {
		//个人-验证登录权限
		if (checkPersonLogin()) {
			next();
		} else {
			next("/person/login/index");
		}
	} else if (to.fullPath.indexOf("/company") >= 0 && to.meta.requiresAuth) {
		if (checkCompanyLogin()) {
			next();
		} else {
			next("/");
		}
	} else if (to.fullPath.indexOf("/common/") >= 0 && to.meta.requiresAuth) {
		if (checkPersonLogin() || checkCompanyLogin()) {
			next();
		} else {
			next("/");
		}
	}
	next();
});

//后置路由守卫 设置title
router.afterEach((to, from) => {
	if (to.meta.title) document.title = to.meta.title;
});